package com.kelvin.oocl.crms.main;

import com.kelvin.oocl.crms.entity.Condition;
import com.kelvin.oocl.crms.entity.Customer;
import com.kelvin.oocl.crms.util.MyLinkList;

/**
 * 对客户做缓存CRUD操作
 * @author MAIKE
 *
 */
public class CustomerDaoImpl implements ICustomerDao{
	private static MyLinkList<Customer> customers = new MyLinkList<>();
	private static int idIncrement=1; //id自动增加
	
	static {
		Customer c10 = new Customer(idIncrement++, "Kelvin", "male", "1994-06-01", "Software_Road_001", "1372222000");
		Customer c1 = new Customer(idIncrement++, "Mike", "male", "1995-07-03", "Software_Road_002", "1372222001");
		Customer c2 = new Customer(idIncrement++, "Nick", "male", "1995-06-04", "Software_Road_003", "1372222002");
		Customer c3 = new Customer(idIncrement++, "Lisa", "female", "1995-06-03", "Software_Road_004", "1372222003");
		Customer c4 = new Customer(idIncrement++, "Allen", "female", "1995-06-09", "Software_Road_005", "1372222004");
		Customer c5 = new Customer(idIncrement++, "Barry", "male", "1995-06-11", "Software_Road_006", "1372222005");
		Customer c6 = new Customer(idIncrement++, "Jack", "female", "1995-08-03", "Software_Road_007", "1372222006");
		Customer c7 = new Customer(idIncrement++, "Zid", "female", "1995-06-015", "Software_Road_008", "1372222007");
		Customer c8 = new Customer(idIncrement++, "Carry", "female", "1995-11-07", "Software_Road_009", "1372222008");
		Customer c9 = new Customer(idIncrement++, "Osca", "male", "1995-06-23", "Software_Road_011", "1372222009");
		customers.add(c10);
		customers.add(c1);
		customers.add(c2);
		customers.add(c3);
		customers.add(c4);
		customers.add(c5);
		customers.add(c6);
		customers.add(c7);
		customers.add(c8);
		customers.add(c9);
	}
	
	@Override
	public MyLinkList<Customer> findAll() {
		return customers;
	}
	

	@Override
	public Customer findById(Integer id) {
		if(id ==null){
			return null;
		}
		try {
		for(int i=0;i<customers.getSize();i++){
			if(customers.get(i).getId()==id){
				return customers.get(i);
			}
		}
		} catch (Exception e) {
			return null;
		}
		return null;
	}

	@Override
	public MyLinkList<Customer> findByConditions(MyLinkList<Condition> conditions){
		if(conditions==null || conditions.getSize()==0){
			return null;
		}
		try {
			MyLinkList<Customer> ctms = new MyLinkList<>();
			String name=null,sex=null,address=null,phone=null,birth=null;
			for (int i = 0; i < conditions.getSize(); i++) {
				String key = conditions.get(i).getKey();
				if("name".equals(key)){
					name = conditions.get(i).getValue();
				}else if("sex".equals(key)){
					sex = conditions.get(i).getValue();;
				}else if("birthDate".equals(key)){
					birth = conditions.get(i).getValue();
				}else if("address".equals(key)){
					address = conditions.get(i).getValue();
				}else if("phoneNumber".equals(key)){
					phone = conditions.get(i).getValue();
				}
			}
			
			for(int i=0;i<customers.getSize();i++){
				boolean flag = true;
				if(name !=null){
					flag = flag && name.equals(customers.get(i).getName());
				}
				if(sex != null){
					flag = flag && sex.equals(customers.get(i).getSex());
				}
				if(address != null){
					flag = flag && address.equals(customers.get(i).getAddress());
				}
				if(birth != null){
					flag = flag && birth.equals(customers.get(i).getBirthDate());
				}
				if(phone != null){
					flag = flag && phone.equals(customers.get(i).getPhoneNumber());
				}
				if(flag){
					ctms.add(customers.get(i));
				}
			}
			return ctms;
		} catch (Exception e) {
			return null;
		}
	}

	@Override
	public boolean add(Customer customer) {
		if(null==customer){
			return false;
		}
		customer.setId(idIncrement++);
		return customers.add(customer);
	}

	@Override
	public boolean deleteById(Integer id) {
		if(null==id){
			return false;
		}
		try {
			for(int i=0;i<customers.getSize();i++){
				if(customers.get(i).getId()==id){
					return customers.remove(customers.get(i));
				}
			}
		} catch (Exception e) {
			return false;
		}
		return false;
	}

	@Override
	public boolean updateById(Customer customer) {
		if(customer==null){
			return false;
		}
		if(customer.getId()==null){
			return false;
		}
		try {
			for (int i = 0; i < customers.getSize(); i++) {
				if(customers.get(i).getId()==customer.getId()){
					Customer c = customers.get(i);
					if(customer.getAddress()!=null){
						c.setAddress(customer.getAddress());
					}
					if(customer.getBirthDate()!=null){
						c.setBirthDate(customer.getBirthDate());
					}
					if(customer.getName() != null){
						c.setName(customer.getName());
					}
					if(customer.getSex() != null){
						c.setSex(customer.getSex());
					}
					if(customer.getPhoneNumber()!=null){
						c.setPhoneNumber(customer.getPhoneNumber());
					}
					return true;
				}
			}
		} catch (Exception e) {
			return false;
		}
		return false;
	}

	@Override
	public MyLinkList<Customer> sortById() {
		try {
			MyLinkList<Customer> csList = new MyLinkList<>();
			for(int i=0;i<customers.getSize();i++){
				customers.get(i).setSort(0);
				csList.add(customers.get(i));
			}
			csList.sort(true);
			return csList;
		} catch (Exception e) {
			System.out.println("  getting customer by index encounter a misake!"+e.getMessage());
			return null;
		}
	}

	@Override
	public MyLinkList<Customer> sortByName() {
		try {
			MyLinkList<Customer> csList = new MyLinkList<>();
			for(int i=0;i<customers.getSize();i++){
				customers.get(i).setSort(1);
				csList.add(customers.get(i));
			}
			csList.sort(true);
			return csList;
		} catch (Exception e) {
			System.out.println("  getting customer by index encounter a misake!"+e.getMessage());
			return null;
		}
	}

}
